package com.imnak0.seminar.displayperformance;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {
	private boolean isConvertView = true;
	private boolean isViewHolder = true;

	private LayoutInflater inflater;
	private ArrayList<CustomArrayType> list;

	public CustomAdapter(Context context, ArrayList<CustomArrayType> list) {
		super();

		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.list = list;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// ViewHolder 미적용 코드
		if (!isViewHolder) {
			if (!isConvertView || convertView == null) {
				convertView = inflater.inflate(R.layout.listview, parent, false);
			}

			ImageView image = (ImageView) convertView.findViewById(R.id.image);
			TextView title = (TextView) convertView.findViewById(R.id.title);
			TextView message = (TextView) convertView.findViewById(R.id.message);

			ImageView table1 = (ImageView) convertView.findViewById(R.id.table1);
			ImageView table2 = (ImageView) convertView.findViewById(R.id.table2);
			ImageView table3 = (ImageView) convertView.findViewById(R.id.table3);
			ImageView table4 = (ImageView) convertView.findViewById(R.id.table4);
			ImageView table5 = (ImageView) convertView.findViewById(R.id.table5);
			ImageView table6 = (ImageView) convertView.findViewById(R.id.table6);
			ImageView table7 = (ImageView) convertView.findViewById(R.id.table7);
			ImageView table8 = (ImageView) convertView.findViewById(R.id.table8);

			image.setImageDrawable(list.get(position).resImage);
			title.setText(list.get(position).title);
			message.setText(list.get(position).message);

			table1.setImageDrawable(list.get(position).resImage);
			table2.setImageDrawable(list.get(position).resImage);
			table3.setImageDrawable(list.get(position).resImage);
			table4.setImageDrawable(list.get(position).resImage);
			table5.setImageDrawable(list.get(position).resImage);
			table6.setImageDrawable(list.get(position).resImage);
			table7.setImageDrawable(list.get(position).resImage);
			table8.setImageDrawable(list.get(position).resImage);
		} else {
			// ViewHolder  적용 코드
			ViewHolder viewHolder;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.listview, parent, false);

//				viewHolder = new ViewHolder();
//				viewHolder.title = (TextView) convertView.findViewById(R.id.title);
//				viewHolder.message = (TextView) convertView.findViewById(R.id.message);
//				viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
//
//				viewHolder.table1 = (ImageView) convertView.findViewById(R.id.table1);
//				viewHolder.table2 = (ImageView) convertView.findViewById(R.id.table2);
//				viewHolder.table3 = (ImageView) convertView.findViewById(R.id.table3);
//				viewHolder.table4 = (ImageView) convertView.findViewById(R.id.table4);
//				viewHolder.table5 = (ImageView) convertView.findViewById(R.id.table5);
//				viewHolder.table6 = (ImageView) convertView.findViewById(R.id.table6);
//				viewHolder.table7 = (ImageView) convertView.findViewById(R.id.table7);
//				viewHolder.table8 = (ImageView) convertView.findViewById(R.id.table8);
//
//				convertView.setTag(viewHolder);
				
				convertView.setTag(R.id.title, convertView.findViewById(R.id.title));
				convertView.setTag(R.id.message, convertView.findViewById(R.id.message));
				convertView.setTag(R.id.image, convertView.findViewById(R.id.image));
				convertView.setTag(R.id.table1, convertView.findViewById(R.id.table1));
				convertView.setTag(R.id.table2, convertView.findViewById(R.id.table2));
				convertView.setTag(R.id.table3, convertView.findViewById(R.id.table3));
				convertView.setTag(R.id.table4, convertView.findViewById(R.id.table4));
				convertView.setTag(R.id.table5, convertView.findViewById(R.id.table5));
				convertView.setTag(R.id.table6, convertView.findViewById(R.id.table6));
				convertView.setTag(R.id.table7, convertView.findViewById(R.id.table7));
				convertView.setTag(R.id.table8, convertView.findViewById(R.id.table8));
			} else {
//				viewHolder = (ViewHolder) convertView.getTag();
			}

//				viewHolder.image.setImageDrawable(list.get(position).resImage);
//				viewHolder.title.setText(list.get(position).title);
//				viewHolder.message.setText(list.get(position).message);
//	
//				viewHolder.table1.setImageDrawable(list.get(position).resImage);
//				viewHolder.table2.setImageDrawable(list.get(position).resImage);
//				viewHolder.table3.setImageDrawable(list.get(position).resImage);
//				viewHolder.table4.setImageDrawable(list.get(position).resImage);
//				viewHolder.table5.setImageDrawable(list.get(position).resImage);
//				viewHolder.table6.setImageDrawable(list.get(position).resImage);
//				viewHolder.table7.setImageDrawable(list.get(position).resImage);
//				viewHolder.table8.setImageDrawable(list.get(position).resImage);
			
			((TextView) convertView.getTag(R.id.title)).setText(list.get(position).title);
			((TextView) convertView.getTag(R.id.message)).setText(list.get(position).message);
			((ImageView) convertView.getTag(R.id.image)).setImageDrawable(list.get(position).resImage);
			
			((ImageView) convertView.getTag(R.id.table1)).setImageDrawable(list.get(position).resImage);
			((ImageView) convertView.getTag(R.id.table2)).setImageDrawable(list.get(position).resImage);
			((ImageView) convertView.getTag(R.id.table3)).setImageDrawable(list.get(position).resImage);
			((ImageView) convertView.getTag(R.id.table4)).setImageDrawable(list.get(position).resImage);
			((ImageView) convertView.getTag(R.id.table5)).setImageDrawable(list.get(position).resImage);
			((ImageView) convertView.getTag(R.id.table6)).setImageDrawable(list.get(position).resImage);
			((ImageView) convertView.getTag(R.id.table7)).setImageDrawable(list.get(position).resImage);
			((ImageView) convertView.getTag(R.id.table8)).setImageDrawable(list.get(position).resImage);
		}
		
		return convertView;
	}
	
	
}
